home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Fish Fillets / script / share / level_dialog.lua < prev    next >
Text File  |  2005-07-16  |  4KB  |  102 lines

  1.  
  2. local DialogState = {
  3.     SOUND_PREFIX = "",
  4.     DEFAULT_LANG = "",
  5.     filename = nil,
  6.     lang = "",
  7.     name = "",
  8.     font = "",
  9.     dialogs = {},
  10. }
  11.  
  12. -- Return path to .ogg file or empty string.
  13. local function dataPathSound(lang, basename)
  14.     local soundFile = DialogState.SOUND_PREFIX..lang.."/"..basename..".ogg"
  15.     if not file_exists(soundFile) then
  16.         soundFile = ""
  17.     end
  18.     return soundFile
  19. end
  20.  
  21. -- Loads localized dialogs from prefix.."dialogs_*.lua"
  22. -- @param prefix prefix of dialogs_<lang>.lua files
  23. -- @param soundPrefix prefix of <lang>/<dialogCodename>.ogg files
  24. -- Default soundPrefix is "sound/<levelCodename>/"
  25. function dialogLoad(prefix, soundPrefix)
  26.     DialogState.SOUND_PREFIX = "sound/"..codename.."/"
  27.     if soundPrefix then
  28.         DialogState.SOUND_PREFIX = soundPrefix
  29.     end
  30.     -- NOTE: uses select_lang.lua to determine avaiable languages
  31.     local langs = {}
  32.     local oldfunc = select_addFlag
  33.     function select_addFlag(lang, flag)
  34.         table.insert(langs, lang)
  35.     end
  36.     file_include("script/select_lang.lua")
  37.     select_addFlag = oldfunc
  38.  
  39.     for key, lang in pairs(langs) do
  40.         local dialogFile = prefix.."dialogs_"..lang..".lua"
  41.         if file_exists(dialogFile) then
  42.             if "" == DialogState.DEFAULT_LANG then
  43.                 DialogState.DEFAULT_LANG = lang
  44.             end
  45.             DialogState.lang = lang
  46.             DialogState.filename = dialogFile
  47.             file_include(dialogFile)
  48.         else
  49.             if string.len(lang) <= 2 then
  50.                 print(string.format("DEBUG: missing translation"..
  51.                     "; lang=%q; file=%q", lang, dialogFile))
  52.             end
  53.         end
  54.     end
  55. end
  56.  
  57. --- Prepares localized dialog and checks for consistency.
  58. -- @param dialogName dialog codename
  59. -- @param fontName font codename (e.g. font_elk, font_parrot, ...)
  60. -- @param defaultSubtitle prime subtitle to translate
  61. function dialogId(dialogName, fontName, defaultSubtitle)
  62.     DialogState.name = dialogName
  63.     DialogState.font = fontName
  64.     local primeDialog = DialogState.dialogs[dialogName]
  65.     if not primeDialog then
  66.         if DialogState.lang == DialogState.DEFAULT_LANG then
  67.             dialogStr(defaultSubtitle)
  68.             DialogState.dialogs[dialogName] = {
  69.                 font = fontName,
  70.                 subtitle = defaultSubtitle,
  71.             }
  72.         else
  73.             print(string.format("WARNING: extra foreign dialog"..
  74.                 "; file=%q; name=%q; subtitle=%q",
  75.                 DialogState.filename, dialogName, defaultSubtitle or ""))
  76.         end
  77.     else
  78.         if primeDialog.font ~= fontName then
  79.             print(string.format("WARNING: bad font for foreign dialog"..
  80.                 "; file=%q; name=%q; primeFont=%q; font=%q",
  81.                 DialogState.filename, dialogName,
  82.                 primeDialog.font or "", fontName or ""))
  83.         end
  84.         if primeDialog.subtitle ~= defaultSubtitle then
  85.             print(string.format(
  86.                 "WARNING: bad defaultSubtitle for foreign dialog"..
  87.                 "; file=%q; name=%q; primeSubtitle=%q; defaultSubtitle=%q",
  88.                 DialogState.filename, dialogName,
  89.                 primeDialog.subtitle or "", defaultSubtitle or ""))
  90.         end
  91.     end
  92. end
  93.  
  94. --- Defines text for localizated dialog
  95. -- @param subtitle localized text
  96. function dialogStr(subtitle)
  97.     dialog_addDialog(DialogState.name, DialogState.lang,
  98.         dataPathSound(DialogState.lang, DialogState.name),
  99.         DialogState.font, subtitle)
  100. end
  101.  
  102.